
import { ESuit, EPokerStatus } from "../../GameScene/ConfigEnum";
import { Poker } from "../../GameScene/GameDB";
import View from "../../../GameFramework/MVC/View";
import GameView from "../../GameScene/GameView/GameView";

const POINT_MAP = {
    "1": "A",
    "2": "2",
    "3": "3",
    "4": "4",
    "5": "5",
    "6": "6",
    "7": "7",
    "8": "8",
    "9": "9",
    "10": "10",
    "11": "J",
    "12": "Q",
    "13": "K",
}
const { ccclass, property } = cc._decorator;

@ccclass
export default class UIPoker extends View {
    @property(cc.Sprite) bgSprite: cc.Sprite = null;
    @property(cc.Sprite) suitSprite: cc.Sprite = null;
    @property(cc.Sprite) smallSprite: cc.Sprite = null;
    @property(cc.Label) pointLabel: cc.Label = null;
    @property(cc.SpriteFrame) textFrontBG: cc.SpriteFrame = null;
    @property(cc.SpriteFrame) textBackBG: cc.SpriteFrame = null;
    @property([cc.SpriteFrame]) bigSuits: cc.SpriteFrame[] = [];
    @property([cc.SpriteFrame]) smallSuits: cc.SpriteFrame[] = [];
    @property([cc.SpriteFrame]) textFaces: cc.SpriteFrame[] = [];

    private redTextColor: cc.Color = cc.color(183, 24, 40);
    private blackTextColor: cc.Color = cc.Color.BLACK;

    private m_poker: Poker = null;
    public get poker(): Poker { return this.m_poker }
    private m_view: GameView = null;

    //--------------------生命周期函数--------------
    public start() {
        console.log('UIPoker :start')
        //注册触摸事件
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this);
    }

    public Init(poker: Poker, view: GameView) {
        this.m_poker = poker;
        this.m_view = view;
        poker.Bind(this);

        //点数
        this.pointLabel.string = "" + POINT_MAP[poker.point];
        //颜色
        this.pointLabel.node.color = (poker.suit == ESuit.HEITAO || poker.suit == ESuit.MEIHUA) ? this.blackTextColor : this.redTextColor;
        //大花色
        if (poker.point >= 11) {
            this.suitSprite.spriteFrame = this.textFaces[poker.point - 11];
        } else {
            this.suitSprite.spriteFrame = this.bigSuits[poker.suit];
        }
        //小花色
        this.smallSprite.spriteFrame = this.smallSuits[poker.suit];
        this.setStatus(poker.status)
    }

    onDestroy() {
        this.node.off(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.off(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.off(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.off(cc.Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this);
    }

    public Refresh() {
        this.setStatus(this.m_poker.status)
    }

    private setStatus(status: EPokerStatus) {
        if (status == EPokerStatus.CLOSE) {
            this.pointLabel.node.active = false;
            this.smallSprite.node.active = false;
            this.suitSprite.node.active = false;
            this.bgSprite.spriteFrame = this.textBackBG;
        } else {
            this.pointLabel.node.active = true;
            this.smallSprite.node.active = true;
            this.suitSprite.node.active = true;
            this.bgSprite.spriteFrame = this.textFrontBG;
        }
    }
    //-------------------public API-------------
    public isOpen(): boolean {
        return this.poker.status == EPokerStatus.OPEN;
    }

    public isPoint(point: number): boolean {
        return this.poker.point == point;
    }

    //------------------------事件处理函数--------------

    onTouchStart(event) {
        let x = this.node.convertToWorldSpaceAR(event).x;
        // console.log('uipoker touchstart')
    }
    onTouchMove(event) {
        let x = this.node.convertToWorldSpaceAR(event).x;
        // console.log('uipoker touchmove')
    }
    onTouchEnd(event) {
        let x = this.node.convertToWorldSpaceAR(event).x;
        this.m_view.OnClickUIPoker(this);
    }
    onTouchCancel(event) {
        let x = this.node.convertToWorldSpaceAR(event).x;
        // console.log('uipoker touchcancel')
    }
}
